<?php

/**
 * Description of ContactController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Contact\Frontend\Controller;

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;
use Library\PHPMailer\PHPMailer;

class ContactController extends BackController{
    
    public function executeContact(HttpRequest $request){
        
        $contacForm = new Form('contact','post');
       
        $contacForm->action('contact.html');
        
        $contacForm->add('Email', 'email')
                   ->label('Email')
                   ->required(true);
        
        $contacForm->add('Select', 'destinataire')
                   ->label('Destinataire')
                   ->choices(array(
                        '' => '--- Choisir un destinataire ---',
                        'serviceclient@kristoclic.fr' => 'Service client',
                        'servicetechnique@kristoclic.fr' => 'Service technique'
                    ))
                    ->required(true);
        $contacForm->add('Text', 'sujet')
                   ->label('Sujet')
                   ->required(true);
        
        $contacForm->add('Textarea', 'message')
                   ->label('Votre demande')
                   ->cols('30')
                   ->rows('7')
                   ->required(true);
        
        $contacForm->add('Captcha', 'captcha')
                   ->label('Captcha')
                   ->required(true);
        
        $contacForm->add('Submit', 'submit')->value('Envoyer');
        $messageError ='';
        if($request->getMethod('post')){
           //test de la validation du post
            if ($contacForm->is_valid($_POST)) {
                //var_dump($_POST);
                list($email_exp, $destinataire, $sujet, $message) = $contacForm->get_cleaned_data('email','destinataire','subjet','message');
                $mail = new PHPMailer();
                $mail->IsSendmail();
                try {
                    //$mail->Body = $message;
                    $mail->AddReplyTo($destinataire);
                    $mail->SetFrom($email_exp);
                    $mail->AddReplyTo($destinataire);
                    $mail->AddAddress($email_exp, "Visitor");
                    $mail->Subject = $sujet;
                    $mail->MsgHTML($message);
                    if($mail->Send()){
                        $messageError = "Votre Message a été envoyé avec succès";
                    }else{
                        $messageError = "echec lors de l'envoi";
                    }
                    
                } catch (phpmailerException $e) {
                  $messageError =  $e->errorMessage(); //Pretty error messages from PHPMailer
                } catch (Exception $e) {
                  $messageError = $e->getMessage(); //Boring error messages from anything else!
                }
            }else{
                // 
                $contacForm->bound($_POST);
            }
        }else{
            $contacForm->bound($_POST);
        }
        $this->page->addVar('contactForm', $contacForm);
        $this->page->addVar('messageError', $messageError);
        $this->page->addVar('title', 'contactez-nous');
    }
}

?>
